//
//  YDSlider.m
//  YunDongGym
//
//  Created by lqq on 2018/6/6.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "YDSlider.h"

@interface YDSlider ()
@property (nonatomic, strong) UIView *valueView;
@property (nonatomic, strong) UIView *indicaterView;
@end

@implementation YDSlider
{
    BOOL _hadSetValue;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self config];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self =[super initWithCoder:aDecoder];
    if (self) {
        [self config];
    }
    return self;
}

- (void)config
{
    UIView *bottomView = [[UIView alloc] init];
    bottomView.userInteractionEnabled = NO;
    [self addSubview:bottomView];
    bottomView.backgroundColor = HEXColor(@"#F4F6F9");
    bottomView.layer.cornerRadius = 6;
    bottomView.layer.masksToBounds = YES;
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(7);
        make.height.equalTo(@(12));
    }];
    
    _valueView = [[UIView alloc] init];
    _valueView.backgroundColor = HEXColor(@"#3AA7FF");
    [bottomView addSubview:_valueView];
    
    
    _indicaterView = [[UIView alloc] init];
    _indicaterView.backgroundColor = [UIColor whiteColor];
    _indicaterView.layer.shadowColor = [UIColor blackColor].CGColor;
    _indicaterView.layer.shadowOpacity = 0.3;
    _indicaterView.layer.cornerRadius = 2;
    _indicaterView.layer.shadowRadius = 1;
    _indicaterView.userInteractionEnabled = NO;
    _indicaterView.layer.shadowOffset = CGSizeMake(0, 0);
    _indicaterView.frame = CGRectMake(0, 0, 12, 26);
    [self addSubview:_indicaterView];
    
    _value = 0.5;
}

-(void)layoutSubviews
{
    if (_value < _minimumValue) {
        _value = _minimumValue;
    }
    if (_value > _maximumValue) {
        _value = _maximumValue;
    }
    if (!_hadSetValue) {
        _value =(_maximumValue + _minimumValue) / 2;
    }
    CGFloat pointX = (_value - _minimumValue) / (_maximumValue - _minimumValue) * self.width;
    _valueView.frame = CGRectMake(0, 0, pointX, 12);
    _indicaterView.centerX = pointX;
}

- (void)setValue:(float)value
{
    _value = value;
    _hadSetValue = YES;
}



/**
 手指滑动时判选中第几个星星
 */
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    NSLog(@"%@",NSStringFromCGPoint(point));
    if (point.x < 0 ) {
        point.x = 0;
    }
    if (point.x > self.width) {
        point.x = self.width;
    }
    _valueView.frame = CGRectMake(0, 0, point.x, 12);
    _indicaterView.centerX = point.x;
    
    _value = (point.x / self.width) * (_maximumValue - _minimumValue) + _minimumValue;
}

/**
 点击结束时判断选择的星星位置
 */
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    NSLog(@"%@",NSStringFromCGPoint(point));
    if (point.x < 0 ) {
        point.x = 0;
    }
    if (point.x > self.width) {
        point.x = self.width;
    }
    _valueView.frame = CGRectMake(0, 0, point.x, 12);
    _indicaterView.centerX = point.x;
    _value = (point.x / self.width) * (_maximumValue - _minimumValue) + _minimumValue;
}
@end
